<template>
  <el-card>
    <div slot="header" class="header">
      <div class="category-header">
        <span>销售额类别占比</span>

        <el-radio-group v-model="modelname">
          <el-radio-button label="线上"></el-radio-button>
          <el-radio-button label="门店"></el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <div>
      <h3>{{ title }}排名</h3>
      <div ref="charts" class="charts"></div>
    </div>
  </el-card>
</template>

<script>
import { mapState } from 'vuex'
import * as echarts from 'echarts'
export default {
  name: '',
  data() {
    return {
      value: '',
      modelname: '线上'
    }
  },
  computed: {
    // 计算属性-标题
    title() {
      // 重新设置配置项
      return this.modelname == '线上' ? '线上' : '线下'
    },

    ...mapState({
      saleRank: (state) => state.Home.list.saleRank
    }),

    online() {
      return this.saleRank.online
    },
    oname() {
      return this.saleRank.online.name
    },
    ovalue() {
      return this.saleRank.online.value
    },
    offline() {
      return this.saleRank.shop
    },
    fname() {
      return this.saleRank.shop.name
    },
    fvalue() {
      return this.saleRank.shop.value
    },

    onlinedata() {
      const prev = []
      this.oname.forEach((item, index) => {
        prev.push({ name: item })
        prev[index].value = this.ovalue[index]
      })

      return prev
    },
    offlinedata() {
      const prev = []
      this.fname.forEach((item, index) => {
        prev.push({ name: item })
        prev[index].value = this.fvalue[index]
      })

      return prev
    }
  },
  watch: {
    saleRank() {
      this.mychart.setOption({
        title: {
          text: '家用电器',
          subtext: 224,
          left: 'center',
          top: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          right: '3%'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: true,
              position: 'outsize'
            },
            labelLine: {
              show: true
            },
            data: this.onlinedata
            // : [
            //   { value: 1048, name: '视频' },
            //   { value: 735, name: 'Direct' },
            //   { value: 580, name: 'Email' },
            //   { value: 484, name: 'Union Ads' },
            //   { value: 300, name: 'Video Ads' }
            // ]
          }
        ]
      })
    },

    title() {
      this.mychart.setOption({
        title: {
          text: '家用电器',
          subtext:
            this.title == '线上'
              ? this.onlinedata[0].value
              : this.offlinedata[0].value,
          left: 'center',
          top: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          right: '3%'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],

            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: true,
              position: 'outsize'
            },
            labelLine: {
              show: true
            },
            data: this.modelname == '线上' ? this.onlinedata : this.offlinedata
            // : [
            //   { value: 1048, name: '视频' },
            //   { value: 735, name: 'Direct' },
            //   { value: 580, name: 'Email' },
            //   { value: 484, name: 'Union Ads' },
            //   { value: 300, name: 'Video Ads' }
            // ]
          }
        ]
      })
    }
  },
  mounted() {
    this.getData()
    // 饼图
    this.mychart = echarts.init(this.$refs.charts)
    window.addEventListener('resize', () => {
      this.mychart.resize()
    })
    // 绑定事件

    this.mychart.on('mouseover', (params) => {
      // 获取鼠标移上去那条数据
      const { name, value } = params.data
      // 重新设置标题
      this.mychart.setOption({
        title: {
          text: name,
          subtext: value
        }
      })
    })
  },
  methods: {
    getData() {
      // const prev = []
      // for (let i = 0; i < this.name.length; i++) {
      //   prev.push({ name: this.name[i] })
      //   for (let i = 0; i < this.value.length; i++) {
      //     prev[i].value = this.value[i]
      //   }
      // }
      // console.log(prev)
    }
  }
}
</script>

<style scoped>
.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.header {
  border-bottom: 1px solid #eee;
}
.charts {
  width: 100%;
  height: 400px;
}
</style>
